package com.oa.android.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oa.android.R;
import com.oa.android.adapter.FujAdapter;
import com.oa.android.adapter.NextAdapter;
import com.oa.android.entry.Fjian;
import com.oa.android.entry.Next;
import com.oa.android.entry.Work;
import com.oa.android.utils.FileMange;
import com.oa.android.utils.ImageUtlis;
import com.oa.android.utils.UConstants;
import com.oa.android.utils.Util;
import com.oa.android.view.MyGridView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 待办工作
 * Created by  on 2016/6/22.
 */
public class DbWrokActivity extends Activity implements View.OnClickListener {

    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.name)
    TextView name;
    @InjectView(R.id.time)
    TextView time;
    @InjectView(R.id.webview)
    WebView webview;
    @InjectView(R.id.yij)
    EditText yij;
    @InjectView(R.id.next)
    TextView next;
    @InjectView(R.id.yes_tong)
    TextView yesTong;
    @InjectView(R.id.no_tong)
    TextView noTong;
    @InjectView(R.id.no_next)
    TextView noNext;
    @InjectView(R.id.workName)
    TextView workName;
    @InjectView(R.id.fujianText)
    TextView fujianText;
    @InjectView(R.id.addFile)
    TextView addFile;

    @InjectView(R.id.layout)
    LinearLayout layout;

//    @InjectView(R.id.gridview)
    private MyGridView gridView;
    private GridView listFj;
    private Work work;
    private String[] fuj;
    private FujAdapter adapter;
    private FileMange fileMange;
    private   String filePath = Environment.getExternalStorageDirectory().getPath() + File.separator+"ZHOA"+ File.separator;
    private Dialog Jdialog;
    private Dialog typeDialog;
    private ProgressDialog progressDialog;
    private List<Next> nextList_tong;
    private List<Next> nextList_boh;
    private String nextJid;
    List<Fjian> list;
    private String sendPerson="";
    private List<String> jdNext;
    private String contentStr;
    private int abc=0;
    private List<File> listFile;
    List<Fjian> listF;
    private FujAdapter adapterF;
    private List<String> memoList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.daiban_activity);
        gridView=(MyGridView)findViewById(R.id.gridview);
        listFj=(GridView)findViewById(R.id.listFj);
        work= (Work) getIntent().getSerializableExtra("work");
        ButterKnife.inject(this);
        titleText.setText("待办审批");
        close.setOnClickListener(this);
        yesTong.setOnClickListener(this);
        noTong.setOnClickListener(this);
        noNext.setOnClickListener(this);
        fileMange=new FileMange(this);
        addFile.setOnClickListener(this);
        listFile = new ArrayList<>();
        listF=new ArrayList<>();
        memoList=new ArrayList<>();
        adapterF=new FujAdapter(DbWrokActivity.this,listF);
        listFj.setAdapter(adapterF);
        getIfNe();
        getFj();
        boHSend();
        WebSettings webSettings = webview.getSettings();
        webSettings.setSupportZoom(true); // 设置是否支持变焦
        webSettings.setLoadWithOverviewMode(true);// 缩放至屏幕大小
        webSettings.setUseWideViewPort(true);// 将图片调整到是个webview的大小
        webSettings.setDefaultTextEncodingName("utf-8");// 设置编码格式
        webSettings.setLoadsImagesAutomatically(true);// 支持自动加载图片
        webSettings.setSupportMultipleWindows(true);// 支持多窗口
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);// 支持js打开新窗口
        webSettings.setDomStorageEnabled(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webview.addJavascriptInterface(new JsInterface(this), "AppView");
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        if (work!=null){
            getNext(work.getID(),UConstants.WORK_NEXT,1,"");
            getNext(work.getWKFID(),UConstants.WORK_UP_JD,2,"");
            workName.setText(work.getWORKNAME());
            time.setText("时间："+work.getOPTIME());
            name.setText("申请人："+work.getEMPNAME());
            String url=UConstants.DZ+"Samples/"+work.getFORMURL()+"l";
            System.out.println(url);
            webview.loadUrl(url);
//            if (!work.getWKZT().equals("1")){
//                layout.setVisibility(View.GONE);
//            }
//            if (Util.NoNull(work.getFUJIANLIST())){
//                if (work.getFUJIANLIST().contains("|")){
//                    fuj=work.getFUJIANLIST().split("\\|");
//                }else{
//                    fuj=new String[]{work.getFUJIANLIST()};
//                }
//                adapter=new FujAdapter(this,list);
//                gridView.setAdapter(adapter);
//                fileMange=new FileMange(this);
//
//            }else {
//                fujianText.setVisibility(View.GONE);
//            }
        }

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {

                super.onPageFinished(view, url);

                super.onPageFinished(view, url);
                JSONObject object=new JSONObject();
                try {
                    object.put("ID",work.getID());
                    object.put("EMPID",work.getEMPID());
                    object.put("EMPNAME",work.getEMPNAME());
                    object.put("CUREMPID",UConstants.EmpId);
                    object.put("CUREMPNAME",UConstants.EmpName);
                    object.put("DEPID",UConstants.DepId);
                    object.put("DEPNAME",UConstants.EmpName);
                    object.put("FS","1");
                    object.put("SPID",work.getSPID());
                    object.put("WKFID",work.getWKFID());
                    object.put("NEMPIDS",work.getNEMPIDS());
                    object.put("WKZT",work.getWKZT());
                    object.put("FORMURL",work.getFORMURL()+"l");
                    Log.i("CCCC",object.toString());

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                webview.loadUrl("javascript: GetContent('"+object.toString()+"')");
            }


        });

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("position===",""+position);
                if (!new File(filePath+list.get(position).getOLDNAME()).exists()){

                    if (Util.NoNull(list.get(position).getNOWNAME())){

                        String[] bs= list.get(position).getNOWNAME().split("/");
                        StringBuffer brf = new StringBuffer();
                        for (int i = 0; i < bs.length; i++) {
                            if(i==bs.length-1){

                                try {
                                    brf.append(URLEncoder.encode(bs[i],"UTF-8").replaceAll("\\+", "%20"));
                                } catch (UnsupportedEncodingException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }else {
                                brf.append(bs[i]).append("/");
                            }

                        }

                        pointDialog("您需要下载此文件吗？",1,brf.toString(),list.get(position).getOLDNAME());
                    }else{
                        Toast.makeText(DbWrokActivity.this,"下载地址有误",Toast.LENGTH_SHORT).show();
                    }

//					downloadFile(UConstants.URL_DOWN+fuj[position],filePath+fuj[position]);
                }else{
                    pointDialog("您需要打开此文件吗？",0,list.get(position).getOLDNAME(),list.get(position).getOLDNAME());
//					openFile(new File(filePath+fuj[position]));
                }

            }
        });

        listFj.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("listFj---",position+"---");
                listF.remove(position);
                listFile.remove(position);
                memoList.remove(position);
                adapterF.notifyDataSetChanged();
                return false;
            }
        });


    }


    /**
     * 提示
     */
    private void pointDialog(String str) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.memo_diog,
                null);
        // 对话框
        Jdialog = new Dialog(this);
        Jdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Jdialog.show();

        Jdialog.setContentView(layout);
        TextView sure = (TextView) layout.findViewById(R.id.sureExti);
        TextView cancle = (TextView) layout.findViewById(R.id.cancleExti);
        TextView dialogTitle = (TextView) layout.findViewById(R.id.dialogTitle);
        final EditText text = (EditText) layout.findViewById(R.id.text);
        dialogTitle.setText(str);
        sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//				ContextUtil.getInstance().finishApp();
                String string=text.getText().toString();
                if(Util.NoNull(string)){
                    memoList.add(string);
                    Jdialog.cancel();
                }else{
                    Toast.makeText(DbWrokActivity.this,"备注不能为空",Toast.LENGTH_SHORT).show();
                }

                Log.i("文件记录保存完成", "============");



            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                memoList.add(" ");

                Jdialog.cancel();
            }
        });

    }

    Handler handler=new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    if(Util.NoNull(contentStr)){
                        if (abc==1){
                            if (null!=listFile&&listFile.size()>0){
                                upFj("2",nextJid,yij.getText().toString());
                            }else{
                                shenHtg("2",nextJid,yij.getText().toString());
                            }

                        }else if (abc==2){
                            if (null!=listFile&&listFile.size()>0){
                                upFj("1","",yij.getText().toString());
                            }else{
                                shenHtg("1","",yij.getText().toString());
                            }

                        }
                    }else{
                        Toast.makeText(DbWrokActivity.this,"内容不能为空",Toast.LENGTH_SHORT).show();
                    }
                    break;

                case 12:
                    pointDialog("备注信息");
                    break;
            }

        }
    };

     class JsInterface {
        private Context mContext;

        public JsInterface(Context context) {
            this.mContext = context;
        }

        // 在js中调用window.AndroidWebView.showInfoFromJs(name)，便会触发此方法。
        @JavascriptInterface
        public void showInfoFromJs(String name) {
            contentStr=name;
            handler.sendEmptyMessage(1);

        }


         @JavascriptInterface
         public void cellPhone(String phone) {

             if (null!=phone&&phone.length()>0){
                 Uri telUri = Uri.parse("tel:" + phone);
                 Intent intent = new Intent(Intent.ACTION_DIAL, telUri);
                 startActivity(intent);
             }else{
                 Toast.makeText(DbWrokActivity.this,"电话不能为空",Toast.LENGTH_SHORT).show();
             }
         }



        @JavascriptInterface
        public String isNetwork() {

            return "00";
        }
    }

    private void getContext(String str,String str1,String str2){

        JSONObject object=new JSONObject();
        try {
            object.put("ID",work.getID());
            object.put("EMPID",work.getEMPID());
            object.put("EMPNAME",work.getEMPNAME());
            object.put("DEPID",UConstants.EmpId);
            object.put("DEPNAME",UConstants.EmpName);
            object.put("FS","1");
            object.put("SPID",work.getSPID());
            object.put("WKFID",work.getWKFID());
            object.put("NEMPIDS",work.getNEMPIDS());
            object.put("WKZT",work.getWKZT());
            object.put("CLBZ",str);
            object.put("NEMPIDS",str2);

            if(abc==2){
                object.put("BKSPID","");
            }else{
                object.put("BKSPID",str1);
            }

            object.put("FORMURL",work.getFORMURL()+"l");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        String string=object.toString();
       System.out.println(string);
        webview.loadUrl("javascript: showInfoFromJava('"+object.toString()+"')");
    }


    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {

        Intent intent;
            switch (v.getId()){

                case R.id.close:
                    finish();
                    break;
                case R.id.next:


                    break;

                case R.id.addFile:
                    intent = new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    startActivityForResult(intent, 1);
                    break;

                case R.id.no_tong:
                    abc=1;
                   if (null!=nextList_boh&&nextList_boh.size()>0){
                       showtype("选择驳回节点");
                   }


                    break;
//                case R.id.no_next:
//
//                    if (nextList_boh!=null&&nextList_boh.size()>0){
//
//                        showtype("选择节点");
//                    }
////                    if (Util.NoNull(nextJid)){
////                        boHSend(work.getID(),yij.getText().toString(),nextJid,2);
////                    }else{
////                        Toast.makeText(getApplicationContext(),"请选择节点!",Toast.LENGTH_SHORT).show();
////                    }
//
//                    break;

                case R.id.yes_tong:
                    abc=2;
                    selectNext("处理工作");

                    break;
            }
    }

    /**
     * 选择节点
     */
    private void showtype(String title) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.sb_state_dailog,
                null);
        // 对话框
        typeDialog = new Dialog(this);
        typeDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        typeDialog.show();

        typeDialog.setContentView(layout);
        TextView textView=(TextView)layout.findViewById(R.id.name);
        textView.setText(title);
        ListView listView = (ListView) layout.findViewById(R.id.statesListView);
        listView.setAdapter(new NextAdapter(this, nextList_boh));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                next.setText(list.get(position).getJIEDIANNAME());
                nextJid= nextList_boh.get(position).getJIEDIANNAME();
                String jj=nextList_boh.get(position).getJIEDIANNAME();
                System.out.println("jj---"+jj);
                typeDialog.dismiss();
                JSONObject object=new JSONObject();
                try {
                    object.put("ID",work.getID());
                    object.put("EMPID",work.getEMPID());
                    object.put("EMPNAME",work.getEMPNAME());
                    object.put("DEPID",UConstants.EmpId);
//                    object.put("DEPNAME",UConstants.EmpName);
                    object.put("FS","2");
                    object.put("SPID",work.getSPID());
                    object.put("WKFID",work.getWKFID());
                    object.put("NEMPIDS",work.getNEMPIDS());
                    object.put("WKZT",work.getWKZT());
                    object.put("CLBZ","2");
                    object.put("BKSPID",jj);
                    object.put("FORMURL",work.getFORMURL()+"l");

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String string=object.toString();
                System.out.println("驳回：----"+string);
        webview.loadUrl("javascript: showInfoFromJava('"+object.toString()+"')");



            }
        });

    }

    /**
     * 提示
     */
    private void pointDialog(String str, final int p, final String pat,final String savePath) {
        LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.exti_dialog,
                null);
        // 对话框
        Jdialog = new Dialog(this);
        Jdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Jdialog.show();

        Jdialog.setContentView(layout);
        TextView sure = (TextView) layout.findViewById(R.id.sureExti);
        TextView cancle = (TextView) layout.findViewById(R.id.cancleExti);
        TextView dialogTitle = (TextView) layout.findViewById(R.id.dialogTitle);
        dialogTitle.setText(str);
        sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
//				ContextUtil.getInstance().finishApp();
                Jdialog.cancel();
                if(p==1){
                    downloadFile(UConstants.URL_DOWN+pat,filePath+savePath);
                }else{
                    fileMange.openFile(new File(filePath+savePath));
                }




            }
        });
        cancle.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Jdialog.dismiss();
            }
        });

    }


    /**
     * 提示
     */
    private void selectNext(String str) {
        final LayoutInflater inflaterDl = LayoutInflater.from(this);
        View layout = inflaterDl.inflate(R.layout.exti_dialog,
                null);
        // 对话框
        Jdialog = new Dialog(this);
        Jdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        Jdialog.show();

        Jdialog.setContentView(layout);
        TextView sure = (TextView) layout.findViewById(R.id.sureExti);
        TextView cancle = (TextView) layout.findViewById(R.id.cancleExti);
        TextView dialogTitle = (TextView) layout.findViewById(R.id.dialogTitle);
        dialogTitle.setText(str);
        sure.setText("保存");
        cancle.setText("下一步骤");

        if(null!=ff&&ff.equals("F")){
            cancle.setTextColor(Color.parseColor("#00a6e4"));
            cancle.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
//				ContextUtil.getInstance().finishApp();
                    Jdialog.cancel();
                    Intent intent=new Intent(DbWrokActivity.this,NextPersonActivity.class);
                    intent.putExtra("wifid",work.getWKFID());
                    intent.putExtra("id",work.getID());
                    intent.putExtra("spid",work.getSPID());
                    intent.putExtra("psmode",PSMODE);
                    startActivityForResult(intent,2);


                }
            });
        }

        sure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Jdialog.dismiss();
                Ns(work.getNEMPIDS());


            }
        });

    }

    private void Ns(String str){

        JSONObject object=new JSONObject();
        try {
            object.put("ID",work.getID());
            object.put("EMPID",work.getEMPID());
            object.put("EMPNAME",work.getEMPNAME());
            object.put("DEPID",UConstants.EmpId);
            object.put("DEPNAME",UConstants.EmpName);
            object.put("FS","2");
            object.put("SPID",work.getSPID());
            object.put("WKFID",work.getWKFID());
            object.put("WKZT",work.getWKZT());
            object.put("CLBZ","1");
            object.put("NEMPIDS",str);
                object.put("BKSPID","");
            object.put("FORMURL",work.getFORMURL()+"l");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        String string=object.toString();
        System.out.println("办理----"+string);
        webview.loadUrl("javascript: showInfoFromJava('"+object.toString()+"')");
    }

    private void downloadFile(final String url, String path) {
        progressDialog = new ProgressDialog(this);
        RequestParams requestParams = new RequestParams(url);
        requestParams.setSaveFilePath(path);
        x.http().get(requestParams, new Callback.ProgressCallback<File>() {
            @Override
            public void onWaiting() {
            }

            @Override
            public void onStarted() {
            }

            @Override
            public void onLoading(long total, long current, boolean isDownloading) {
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMessage("下载中...");
                progressDialog.show();
                progressDialog.setMax((int) total);
                progressDialog.setProgress((int) current);
            }

            @Override
            public void onSuccess(File result) {
                Toast.makeText(DbWrokActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ex.printStackTrace();
                Toast.makeText(DbWrokActivity.this, "文件不存在或网络问题！", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }


    //获取附件
    public void getFj() {
        Util.printLog("------------UConstants.GET_ID-----", UConstants.GET_ID);
        progressDialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(UConstants.GET_FJLIST);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("TNAME", "ERPGZGL");

        params.addBodyParameter("XH", work.getID());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<Fjian>>() {
                            }.getType();
                            list = gson.fromJson(object.getString("rows").toString(), lt);
                            if (list != null && list.size() > 0) {
                                adapter=new FujAdapter(DbWrokActivity.this,list);
                                gridView.setAdapter(adapter);
                            }


                        } else {
                            Toast.makeText(x.app(), "获取附件失败！", Toast.LENGTH_LONG).show();

                        }
//
                    } catch (JSONException e) {

                        Toast.makeText(x.app(), "获取附件失败！", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
                progressDialog.dismiss();

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取附件失败！", Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                progressDialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //获取节点列表
    private void getNext(String id, String url, final int type1,String jdId) {
//        progressDialog = ProgressDialog.show(this, null, "加载中...", true, false);
        RequestParams params = new RequestParams(url);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("USNAME", UConstants.EmpName);
        if (type1==1){
            params.addBodyParameter("ID", id);
        }else{
            params.addBodyParameter("WorkFlowID", id);
            params.addBodyParameter("JieDianID", jdId);
        }

//        params.addBodyParameter("PAGE", page + "");
//        params.addBodyParameter("ROWS", pageSize + "");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
//                        allPage = Integer.parseInt(object.getString("total"));
//                        allPage = (int) Math.ceil((double) allPage / pageSize);
                        boolean success = object.getBoolean("success");
//                        String msg = object.getString("msg");
                        if (success) {
                            Gson gson = new Gson();
                            Type lt = new TypeToken<List<Next>>() {
                            }.getType();
                            Util.printLog("type1-----------------", type1+"");
                            if (type1==1){
                                nextList_tong = gson.fromJson(object.getString("rows").toString(), lt);
                                if (nextList_tong !=null&& nextList_tong.size()>0){
//                                    next.setVisibility(View.VISIBLE);
                                    yesTong.setText("通过下一个节点");
                                }else{
                                    yesTong.setText("通过");
                                }
                            }else{

                                nextList_boh= gson.fromJson(object.getString("rows").toString(), lt);
                                if (nextList_boh !=null&& nextList_boh.size()>0){
//                                    next.setVisibility(View.VISIBLE);
                                    noNext.setVisibility(View.VISIBLE);
                                }else{
                                    Util.printLog("AAAAAAAAAAAAAAAA-----------------", "");
                                }
                            }


                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
//                progressDialog.dismiss();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取节点失败！", Toast.LENGTH_LONG).show();
//                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
//                progressDialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    //驳回发件人和节点人
    private void boHSend() {

        RequestParams params;

            params = new RequestParams(UConstants.BOHUI_NEXT);


        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("ID", work.getWKFID());
        params.addBodyParameter("SPID", work.getSPID());


        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    nextList_boh=new ArrayList<Next>();
                    try {
                        JSONArray object = new JSONArray(result);

                     if (object.length()>0){
                         jdNext=new ArrayList<String>();
                         for (int i=0;i<object.length();i++){
                             Next next=new Next();
                             next.setJIEDIANNAME(object.getJSONObject(i).getString("SPID"));
//                             jdNext.add(object.getJSONObject(i).getString("SPID"));
                             nextList_boh.add(next);
                         }
                     }
//
//
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "获取驳回节点出错！", Toast.LENGTH_LONG).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                progressDialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }



    //审核通过
    private void shenHtg(String clbz,String nextss,String adv) {
        if (TextUtils.isEmpty(contentStr)||contentStr.length()<80){
            Toast.makeText(DbWrokActivity.this,"获取网页表单数据不全",Toast.LENGTH_SHORT).show();
            progressDialog.dismiss();
            return;
        }

        RequestParams params = new RequestParams(UConstants.BAN_LI_WORK);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("ID",work.getID());
        params.addBodyParameter("WKFID",work.getWKFID());
        params.addBodyParameter("SPID",work.getSPID());
        params.addBodyParameter("CONTENT",contentStr);
        params.addBodyParameter("NDEMPIDS",nextJid);
        params.addBodyParameter("CLBZ",clbz);
        params.addBodyParameter("BKSPID",nextss);
        params.addBodyParameter("ADVICE",adv);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    progressDialog.dismiss();
                    try {
                        JSONObject object = new JSONObject(result);

                        boolean success = object.getBoolean("success");
//
                        if (success) {

                            Toast.makeText(x.app(), "操作成功！", Toast.LENGTH_LONG).show();
                            WorkListActivity.activity.setRef();
                            finish();

                        } else {

                            Toast.makeText(x.app(), object.getString("msg")+"", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "操作失败！", Toast.LENGTH_LONG).show();
                progressDialog.dismiss();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                progressDialog.dismiss();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    private Uri mPhotoUri = null;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {//是否选择，没选择就不会继续
            if (requestCode==1){
                Uri uri = data.getData();
                String  path= ImageUtlis.getRealPathFromUri(this,uri);
                if (null==path||path.length()<=0){
                    Toast.makeText(this,"获取文件失败",Toast.LENGTH_SHORT).show();
                    return;
                }
                File file = new File(path);
                listFile.add(file);

                Fjian fjian=new Fjian();
                fjian.setOLDNAME(file.getName());
                listF.add(fjian);
                adapterF.notifyDataSetChanged();
                handler.sendEmptyMessage(12);
            } else
             if (requestCode==2){
                if (data != null) {
//                    nextJid= data.getStringExtra("person");
                    nextJid=data.getStringExtra("personID");
                    Log.i("personID---",nextJid);
//                  sendTo.setText(str);
                    Ns(nextJid);

                }
            }

//
        }
    }




    //上传附件
    public void upFj(final String clbz, final String nextJid, final String adv) {

        progressDialog = ProgressDialog.show(this, null, "处理中...", true, false);
        StringBuffer stringBuffer=new StringBuffer();
        for (int j=0;j<memoList.size();j ++){
            if (stringBuffer.toString().length()>0){
                stringBuffer.append(",").append(memoList.get(j));
            }else {
                stringBuffer.append(memoList.get(j));
            }
        }
        RequestParams params = new RequestParams(UConstants.UP_FILI);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("TNAME", "ERPGZGL");
        params.addBodyParameter("XH",work.getID() );
        params.addBodyParameter("MEMO", stringBuffer.toString());
        Util.printLog("stringBuffer.toString()-----", stringBuffer.toString());
        Util.printLog("UConstants.UP_FILI------------onSuccess-----", UConstants.UP_FILI);
        for (int i=0;i<listFile.size();i++){
            params.addBodyParameter("FILE",listFile.get(i));
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");

//                        String msg = object.getString("msg");
                        if (success) {
                            shenHtg(clbz,nextJid,adv);

                        } else {
                            isShow();
                            Toast.makeText(x.app(), "附件上传失败！", Toast.LENGTH_LONG).show();
                        }
//
                    } catch (JSONException e) {
                        isShow();
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(),  "附件上传出错！", Toast.LENGTH_LONG).show();
                isShow();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
                isShow();
            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void isShow(){
        if (null!=progressDialog&&progressDialog.isShowing()){
            progressDialog.dismiss();
        }
    }


    private String ff;
    private String PSMODE;

    //是否有下一个审核人
    public void getIfNe() {
        Util.printLog("------------UConstants.GET_ID-----", UConstants.IF_SS_F);
        RequestParams params = new RequestParams(UConstants.IF_SS_F);
        params.addBodyParameter("LKind", "OA");
        params.addBodyParameter("WKFID", work.getWKFID());
        params.addBodyParameter("EmpId", UConstants.EmpId);
        params.addBodyParameter("ID", work.getWKFID());
        params.addBodyParameter("SPID", work.getSPID());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getDownRoute------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
                        String msg = object.getString("msg");
                        if (success) {
                            ff = msg;
                            PSMODE=object.getString("PSMODE");
                        } else {
                            Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();

                        }
//
                    } catch (JSONException e) {

                        Toast.makeText(x.app(), "查询失败！", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }


            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "查询失败！", Toast.LENGTH_LONG).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
